﻿using System;
using System.Web;
using EPiServer;
using TemplateFoundation.PageTypes;

namespace TemplateFoundation.WebControls
{
    /// <summary>
    /// Can be used instead of the standard EPiServer property control to include outer and inner container elements
    /// </summary>
    public class Property : EPiServer.Web.WebControls.Property
    {
        /// <summary>
        /// Gets or sets the outer container element
        /// </summary>
        public string ContainerElement { get; set; }

        /// <summary>
        /// Gets or sets the CSS class of the outer container element
        /// </summary>
        public string ContainerCssClass { get; set; }

        /// <summary>
        /// Gets or sets the inner container element if any
        /// </summary>
        public string InnerContainerElement { get; set; }

        /// <summary>
        /// Gets or sets the CSS class of the inner container element
        /// </summary>
        public string InnerContainerCssClass { get; set; }

        /// <summary>
        /// Renders the control to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if(InnerProperty==null || InnerProperty.IsNull)
            {
                Visible = false;
                return;
            }

            // Check if container element and container CSS class have been specified
            bool containerElement = !string.IsNullOrEmpty(ContainerElement);
            bool containerCssClass = !string.IsNullOrEmpty(ContainerCssClass);

            // Check if inner container element and container CSS class have been specified
            bool innerContainerElement = !string.IsNullOrEmpty(InnerContainerElement);
            bool innerContainerCssClass = !string.IsNullOrEmpty(InnerContainerCssClass);

            // Render container element(s) if specified
            if (containerElement)
            {
                if (containerCssClass)
                {
                    writer.Write(string.Format("<{0} class=\"{1}\">", ContainerElement, ContainerCssClass));
                }
                else
                {
                    writer.Write(string.Format("<{0}>", ContainerElement));        
                }

                if (innerContainerElement)
                {
                    if (innerContainerCssClass)
                    {
                        writer.Write(string.Format("<{0} class=\"{1}\">", InnerContainerElement, InnerContainerCssClass));
                    }
                    else
                    {
                        writer.Write(string.Format("<{0}>", InnerContainerElement));
                    }
                }
            }

            // Special rules for DateTime properties to allow formatting
            if (PropertyValue is DateTime && !string.IsNullOrEmpty(DateFormat))
            {
                writer.Write(((DateTime)PropertyValue).ToString(DateFormat));
            }
            else
            {
                base.Render(writer);    
            }
            

            // Close container element(s), if any
            if (containerElement)
            {
                if(innerContainerElement)
                {
                    writer.Write(string.Format("</{0}>", innerContainerElement));    
                }

                writer.Write(string.Format("</{0}>", ContainerElement));
            }
        }

        /// <summary>
        /// Gets or sets the date format for DateTime property types
        /// </summary>
        public string DateFormat { get; set; }

        /// <summary>
        /// Gets the current page
        /// </summary>
        protected virtual PageTypeBase CurrentPage
        {
            get
            {
                return (HttpContext.Current.Handler as PageBase).CurrentPage as PageTypeBase;
            }
        }
    }
}
